package hacked;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class ReduceVerbosity {
	public static void println(Object o)
	{
		System.out.println(o);
	}
	public static void print(Object object)
	{
		System.out.print(object);
	}
	public static Iterable<Integer> range(final int start,final int end)
	{
		return new Iterable<Integer>() {
			
			@Override
			public Iterator<Integer> iterator() {
				return new Iterator<Integer>() {
					int s = start;
					int e = end;
					@Override
					public boolean hasNext() {
						
						return s<e;
					}

					@Override
					public Integer next() {
						return s++;
					}

					@Override
					public void remove() {
						
					}
				};
			}
		};
	}
	public static String readLine(){
		return new Scanner(System.in).nextLine();
	}
	public static int readInt()
	{
		return new Integer(new Scanner(System.in).nextLine());
	}
	public static <T> List<T> tolist(T...t)
	{
		List<T> list = new ArrayList<T>();
		for(T a:t)
			list.add(a);
		return list;
	}
	public static Iterable<Integer> range(int end)
	{
		return range(0,end);
	}
	public static <T> List<T> list(Iterable<T> i)
	{
		List<T>rv=new ArrayList<>();
		for(T t:i)
			rv.add(t);
		return rv;
	}
	/**
	 * Read an object from a file, null if something goes wrong
	 */
	public static <T> T readObject(Class<T> clazz,String s) 
	{
		T rv;
			 try {
				rv= clazz.cast(new ObjectInputStream(new FileInputStream(new File(s))).readObject());
			} catch (Exception e)
			{
				throw new RuntimeException(e);
			}
		return rv;
	}
	/**
	 * Writeobject ( should be serializable)
	 */
	public static void writeObject(Object o,String s) 
	{
		
		try {
			new ObjectOutputStream(new FileOutputStream(new File(s))).writeObject(o);
		} catch (Exception e){
				throw new RuntimeException(e);
		}
	}
	public static abstract class Folder<A,B>
	{
		B register;

		public Folder(B register)
		{
			this.register=register;
		}
		public abstract B apply(A a,B b);
		
		public B result(){return register;}
	}
	public static <A,B> B fold(Folder<A, B> f ,Iterable<A> list)
	{
		for(A a:list)
			f.register =f.apply(a,f.register);
		return f.register;
	}
	public static interface Filter<T>
	{
		public boolean accepts(T t);
	}
	public static <A> List<A> filter(List<A> list,final Filter<A> filter)
	{
		return fold(new Folder<A,List<A>>(new ArrayList<A>()){

			@Override
			public List<A> apply(A a, List<A> b) {
				if(filter.accepts(a))
					b.add(a);
				return b;
			}},list);
	}
}